const query = require("../db/mysql")
const { success, error } = require("../module/module")


class Information {
    //搜索
    searchInfore = async (ctx) => {
        let { title } = ctx.request.body
        let sql = `select * from information where 1=1 `
        if (title) {
            sql += `and title like "%${title}%"`
        }
        let data = await query(sql)
        if (data.length) {
            ctx.body = success(data, '搜索成功')
        } else {
            ctx.body = error('没有类似数据')
        }
    }

    //筛选
    selectInfore = async (ctx) => {
        let { state } = ctx.request.body
        let sql = `select * from information where 1=1 `
        if (state) {
            sql += `and state like "%${state}%"`
        }
        let data = await query(sql)
        if (data.length) {
            ctx.body = success(data, '筛选成功')
        } else {
            ctx.body = error('没有类似数据')
        }
    }

    //新增
    addInfore = async (ctx) => {
        let { title, name, time, state } = ctx.request.body
        let homeSql = `select * from information where 1=1  and title like "%${title}%"  `
        let isHave = await query(homeSql)
        if (isHave.length) {
            ctx.body = error('已存在此房源')
        } else {
            let sql = `insert into information (title, name, time, state ) values ("${title}","${name}","${time}","${state}") `
            let data = await query(sql)
            if (data.affectedRows === 1) {
                ctx.body = success(data, '添加成功')
            } else {
                ctx.body = error('添加失败')
            }
        }
    }

    //获取数据
    getInfore = async (ctx) => {
        let { pageIndex, pageSize } = ctx.query
        let sql = `select * from information where 1=1 `
        let countSql = 'select count(id) as total from information '
        let total = await query(countSql)
        if (pageIndex * 1) {
            sql += `limit ${(pageIndex * 1 - 1) * pageSize * 1},${pageSize * 1} `
        }
        let data = await query(sql)
        ctx.body = {
            code: 1,
            total: total[0].total,
            data
        }
    }

    //数据回显
    backInfore = async (ctx) => {
        let { id } = ctx.request.body
        let sql = `select * from information where 1=1 `
        if (id) {
            sql += `and id=${id}`
        }
        let data = await query(sql)
        if (data.length) {
            ctx.body = success(data)
        }
    }

    //编辑
    changeInfore = async (ctx) => {
        let { id, title, name, time, state } = ctx.request.body
        let sql = `update information set title='${title}' , name='${name}',time='${time}' , state='${state}' where id=${id} `
        let data = await query(sql)
        if (data.affectedRows === 1) {
            ctx.body = success(data, '更新成功')
        } else {
            ctx.body = error('更新失败')
        }
    }
    //删除
    deleteInfore = async (ctx) => {
        let { id } = ctx.request.body
        let sql = `delete  from information where 1=1 and id=${id} `
        let data = await query(sql)
        if (data.affectedRows === 1) {
            ctx.body = success(data, '删除成功')
        } else {
            ctx.body = error('删除失败')
        }
    }
}


module.exports = new Information()